//
//  HDTimeLineView.swift
//  HuaDaoPeifu
//
//  Created by 刘汉琳 on 16/4/8.
//  Copyright © 2016年 paiyipai. All rights reserved.
//

import UIKit

class HDTimeLineView: UIView {
    
    var timeArray = [HDPayoutoptLogModel]()
    var uploadTimeStr:Int = 0
    var status:String = ""

    init(){
        super.init(frame: CGRect.zero)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    
    func setupTimeLineView(){
        
        var height_Y:CGFloat = 10
        if uploadTimeStr > 0 {
            let timeLabel = UILabel()
            self.addSubview(timeLabel)
            timeLabel.snp.makeConstraints { (make) in
                make.top.equalTo(height_Y)
                make.left.equalTo(30)
                make.width.equalTo(AppConstant.screenWidth - 40)
                make.height.equalTo(13)
            }
            timeLabel.text =  dateString(String(uploadTimeStr), dateFormat: "yyyy-MM-dd HH:mm")
            timeLabel.textAlignment = .left
            timeLabel.font = UIFont.systemFont(ofSize: 13)
            timeLabel.textColor = UIColor(hex:0x666666)
            height_Y += 30
        }
    
        if timeArray.count>0 {
            let lineLabel = UILabel()
            self.addSubview(lineLabel)
            lineLabel.snp.makeConstraints { (make) in
                make.top.equalTo(0)
                make.left.equalTo(18)
                make.width.equalTo(1)
                make.height.equalTo(height_Y)
            }
            lineLabel.backgroundColor = UIColor(hex:0xd5d5d5)
            
        }
        
        for index in 0..<timeArray.count {
            if index != 0 {
                createLine(height_Y - 40)

            }
            if index == 0 {
                if (status == "1" || status == "6") && (timeArray[index].status == "1" || timeArray[index].status == "6")  {
                    
                }else{
                    if status == timeArray[index].status &&  status != "" {
                        
                    }else{
                        createPoint(height_Y)
                        createNameLabel(height_Y - 5, textStr: timeArray[index].title,font:18)
                        createNameLabel(height_Y + 18, textStr: dateString(timeArray[index].created, dateFormat: "yyyy-MM-dd HH:mm"),font:13 )
                        
                        height_Y += 50
                    }
                }
                
            }else{
                createPoint(height_Y)
                createNameLabel(height_Y - 5, textStr: timeArray[index].title,font:18)
                createNameLabel(height_Y + 18, textStr: dateString(timeArray[index].created, dateFormat: "yyyy-MM-dd HH:mm"),font:13 )
                
                height_Y += 50
            }
           
            
        }
        
        self.frame = CGRect(x: 0, y: 0, width: AppConstant.screenWidth, height: height_Y)

        
    }
    
    func createPoint(_ point_Y:CGFloat){
        let pointLabel = UILabel()
        
        self.addSubview(pointLabel)
        pointLabel.snp.makeConstraints { (make) in
            make.top.equalTo(point_Y)
            make.left.equalTo(15)
            make.width.equalTo(7)
            make.height.equalTo(7)
        }
        pointLabel.layer.cornerRadius = 3.5
        pointLabel.layer.masksToBounds = true
        pointLabel.backgroundColor = UIColor(hex:0xd5d5d5)
        
    }
    
    func createNameLabel(_ nameLable_Y:CGFloat,textStr:String,font:CGFloat){
        let nameLabel = UILabel()
        self.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(nameLable_Y)
            make.left.equalTo(30)
            make.width.equalTo(AppConstant.screenWidth - 40)
            make.height.equalTo(font)
        }
        nameLabel.text = textStr
        nameLabel.textAlignment = .left
        nameLabel.font = UIFont.systemFont(ofSize: font)
        nameLabel.textColor = UIColor(hex:0x999999)
    }
    
    func createLine(_ line_Y:CGFloat){
        let lineLabel = UILabel()
        
        self.addSubview(lineLabel)
        lineLabel.snp.makeConstraints { (make) in
            make.top.equalTo(line_Y - 5)
            make.left.equalTo(18)
            make.width.equalTo(1)
            make.height.equalTo(45)
        }
        lineLabel.backgroundColor = UIColor(hex:0xd5d5d5)
    }
    
    func dateString(_ dateStr:String,dateFormat:String)->String{
        
        let timeSta:TimeInterval = dateStr.doubleValue!
        let dfmatter = DateFormatter()
        dfmatter.dateFormat = dateFormat
        
        let date = Date(timeIntervalSince1970: timeSta)
        
        return dfmatter.string(from: date)
    }

}
